// pages/loanEnd/loanEnd.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    list2: [],
    active: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    let data = JSON.parse(options.data)
    that.principal(data)
    let {
      interest,
      year,
      monney
    } = data;
   
    // console.log(data)
    monney = Number(monney) * 10000 //总贷款额
    let MonthlyInterest = interest / 12; //月利率
    let monthly = year * 12; //还款月数
    let list = this.data.list;
    let listMonney = ((monney * (interest / 12) * (1 + (interest / 12)) ** monthly) / ((1 + (interest / 12)) ** monthly - 1)).toFixed(2)
    let num = (((monney * MonthlyInterest - listMonney) * ((1 + MonthlyInterest) ** monthly - 1) / MonthlyInterest + monthly * listMonney) / 10000).toFixed(2)
    for (let i = 1; i <= (year * 12); i++) {
      let monney2 = (monney * MonthlyInterest * (1 + MonthlyInterest) ** monthly) / ((1 + MonthlyInterest) ** monthly - 1) - (monney * MonthlyInterest)
      monthly = monthly - 1
      let arr = {}
      arr.index = i
      arr.monney = listMonney
      arr.principal = monney2.toFixed(2)//月供本金
      arr.surplus = (monney - monney2).toFixed(2).replace("-", "")//剩余本金
      arr.interest = (monney * MonthlyInterest).toFixed(2) // 利息
      list.push(arr)
      monney = monney - monney2
    }
    // let monney3;
    // monney3 = (monney / monthly + monney * MonthlyInterest) + monney / monthly * (1 + MonthlyInterest) / 2 * monthly - monney;
    // console.log(monney3)
    this.setData({
      list: list,
      monney: data.monney,
      year: data.year,
      num: num
    })
  },
  principal(data){
    let that = this;
    let list = that.data.list2;
   let interest = data.interest;
   let year = data.year;
   let monney = data.monney
    monney = Number(monney) * 10000 //总贷款额
    let monney2 =monney; 
    let MonthlyInterest = interest / 12; //月利率
    let monthly = year * 12; //还款月数
    let monney3 = ((monney/monthly)+(monney*MonthlyInterest) + (monney/monthly*(1+MonthlyInterest)))/2*monthly-monney
    for (let i = 1; i <= (year * 12); i++) {
      let arr2 = {};
      arr2.index = i
      arr2.principal =( monney / monthly).toFixed(2)//月供本金
      arr2.interest = (monney2 * MonthlyInterest).toFixed(2) //月供利息
      arr2.surplus = (Number(monney2) - Number(arr2.principal)).toFixed(2) //剩余本金
      arr2.monney = ( Number(arr2.principal) +Number( arr2.interest) ).toFixed(2)//月供总额
      list.push(arr2)
      monney2 = arr2.surplus
    }
   that.setData({
     list2:list,
     onMonney:(list[0].monney - list[1].monney).toFixed(2),
     monney3:(monney3/10000).toFixed(2)
   })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})